package fr.sedoo.commons.client.faq.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import fr.sedoo.commons.client.component.AbstractView;
import fr.sedoo.commons.client.faq.bundle.FaqMessages;
import fr.sedoo.commons.client.util.LocaleUtil;
import fr.sedoo.commons.client.util.StringUtil;
import fr.sedoo.commons.client.widget.richtexteditor.RichTextEditor;
import fr.sedoo.commons.shared.domain.faq.QuestionAndAnswer;

public class FaqEditViewImpl extends AbstractView implements FaqEditView {

	private final static int QUESTION_LENGTH=120;
	
	private static FaqEditViewImplUiBinder uiBinder = GWT
			.create(FaqEditViewImplUiBinder.class);

	interface FaqEditViewImplUiBinder extends UiBinder<Widget, FaqEditViewImpl> {
	}
	
	HashMap<String, TextBox> questionsEditors = new HashMap<String, TextBox>();
	HashMap<String, RichTextEditor> answersEditors = new HashMap<String, RichTextEditor>();
	
	@UiField
	VerticalPanel mainPanel;
	
	Presenter presenter;
	private List<String> languages = new ArrayList<String>();

	public FaqEditViewImpl() {
		super();
//		GWT.<GlobalBundle>create(GlobalBundle.class).css().ensureInjected();
		initWidget(uiBinder.createAndBindUi(this));
		applyCommonStyle();
	}

	@Override
	public void setContent(HashMap<String, QuestionAndAnswer> questions, List<String> languages) {
		this.languages  = languages;
		reset();
		if (languages.size()==1)
		{
			String language = languages.get(0);
			QuestionAndAnswer questionAndAnswer = questions.get(language);
			if (questionAndAnswer== null)
			{
				questionAndAnswer = new QuestionAndAnswer();
			}
			TextBox questionEditor = new TextBox();
			questionEditor.setVisibleLength(QUESTION_LENGTH);
			RichTextEditor answerEditor = new RichTextEditor();
			answerEditor.setWidth("100%");
			questionsEditors.clear();
			questionsEditors.put(language, questionEditor);
			questionEditor.setText(StringUtil.trimToEmpty(questionAndAnswer.getQuestion()));
			answersEditors.clear();
			answersEditors.put(language, answerEditor);
			answerEditor.setHTML(StringUtil.trimToEmpty(questionAndAnswer.getAnswer()));
			
			VerticalPanel panel = new VerticalPanel();
			panel.setWidth("100%");
			panel.setHeight("100%");
			panel.add(new Label(FaqMessages.INSTANCE.question()));
			panel.add(questionEditor);
			panel.add(new Label(FaqMessages.INSTANCE.answer()));
			panel.add(answerEditor);
			mainPanel.add(panel);
			mainPanel.setHeight("100%");
		}
		else
		{
			TabPanel tabPanel = new TabPanel();
			tabPanel.setWidth("100%");
			tabPanel.setHeight("100%");
			answersEditors.clear();
			questionsEditors.clear();
			Iterator<String> iterator = languages.iterator();
			while (iterator.hasNext()) 
			{
				String language = (String) iterator.next();
				QuestionAndAnswer questionAndAnswer = questions.get(language);
				if (questionAndAnswer== null)
				{
					questionAndAnswer = new QuestionAndAnswer();
				}
				TextBox questionEditor = new TextBox();
				questionEditor.setVisibleLength(QUESTION_LENGTH);
				RichTextEditor answerEditor = new RichTextEditor();
//				answerEditor.addToolBar(new ResifRichTextToolbar(answerEditor.getRichTextArea()) );
				questionsEditors.put(language, questionEditor);
				questionEditor.setText(StringUtil.trimToEmpty(questionAndAnswer.getQuestion()));
				answersEditors.put(language, answerEditor);
				answerEditor.setHTML(StringUtil.trimToEmpty(questionAndAnswer.getAnswer()));
				
				answerEditor.setWidth("100%");
				answerEditor.setHeight("100%");
				
				VerticalPanel panel = new VerticalPanel();
				panel.setWidth("100%");
				panel.setHeight("100%");
				panel.add(new Label(FaqMessages.INSTANCE.question()));
				panel.add(questionEditor);
				panel.add(new Label(FaqMessages.INSTANCE.answer()));
				panel.add(answerEditor);
				
				tabPanel.add(panel, LocaleUtil.getLanguageFlag(language));
			}
			tabPanel.selectTab(0);
			mainPanel.add(tabPanel);
			mainPanel.setHeight("100%");
		}
	}

	@Override
	public void reset() {
		mainPanel.clear();
	}

	@UiHandler("saveButton")
	void onSaveButtonClicked(ClickEvent event)
	{
		HashMap<String, QuestionAndAnswer> result = new HashMap<String, QuestionAndAnswer>();
		Iterator<String> iterator = languages.iterator();
		while (iterator.hasNext()) 
		{
			String language = (String) iterator.next();
			String question = StringUtil.trimToEmpty(questionsEditors.get(language).getText());
			String answer = StringUtil.trimToEmpty(answersEditors.get(language).getHTML());
			result.put(language, new QuestionAndAnswer(question, answer));
		}
		
		
//		Iterator<Entry<String, RichTextEditor>> iterator = editors.entrySet().iterator();
//		while (iterator.hasNext()) {
//			Entry<String, RichTextEditor> entry = (Entry<String, RichTextEditor>) iterator.next();
//			result.put(entry.getKey(), entry.getValue().getHTML());
//		}
		presenter.save(result);
	}
	
	@UiHandler("backButton")
	void onBackButtonClicked(ClickEvent event)
	{
		presenter.back();
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}
}
